<?php
namespace Home\Controller;
use Think\Controller;
class MemberController extends Controller {
	public function _initialize()
    {
        //未登录、登录超时、长时间未动
    	if (empty($_SESSION['uid']) || time() > $_SESSION['overtime'] || empty($_SESSION['overtime']) ) {

    		$this->redirect('Common/login');
    	}

        session('overtime',(time()+60*60*C('overtime')));

        // 用户信息
        
        
    }

    /**
     * [givingregister 赠送注册积分]
     * @return [type] [description]
     */
    public function givingregister(){
        $uid = $_SESSION['uid'];

        $m_user = M('User');
        $where['id'] = $uid;

        $userInfo = $m_user->where($where)->find();

        $this->assign('userInfo',$userInfo);
        $this->display();
    }


    /**
     * [givingtask 赠送动态秘钥]
     * @return [type] [description]
     */
    public function givingtask(){

        $uid = $_SESSION['uid'];

        $m_user = M('User');
        $m_giving = M('Giving');

        $where['id'] = $uid;
        $userInfo = $m_user->where($where)->find();

        $where_give['user_id'] = $uid;
        $count = $m_giving->where($where_give)->count();
        $Page  = new \Think\Page($count,20);
        $show  = $Page->show();

        $givingList = $m_giving->where($where_give)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign('userInfo',$userInfo);
        $this->assign('page',$show);
        $this->assign('givingList',$givingList);
        $this->display();
    }


    /**
     * [ctrl_getReceive 获取转出账号姓名]
     * @return [type] [description]
     */
    public function ctrl_getReceive(){
        if (IS_AJAX) {
            $account = trim($_REQUEST['account']);

            $m_user = M('User');

            $where_user['account'] = $account;
            $accountInfo = $m_user->where($where_user)->getfield('truename');
            
            if ($accountInfo) {
                $this->ajaxReturn(array(
                    'code'=>1,
                    'msg' => '查询账号成功',
                    'name'=>$accountInfo
                ));
            }else{
                $this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '无此账号'
                ));
            }
        }else{
            $this->redirect('Common/login');
        }
    }


    /**
     * [ctrl_giving 赠送动态秘钥]
     * @return [type] [description]
     */
    public function ctrl_giving(){
        if (IS_AJAX) {
            $uid = $_SESSION['uid'];
            // print_r($_REQUEST);die();

            $give_account = trim($_REQUEST['account']);
            $give_num = trim($_REQUEST['num']);
            $safe_password = trim($_REQUEST['safe_password']);

            $m_user = M('User');

            $where_user['id'] = $uid;
            $userInfo = $m_user->where($where_user)->find();

            if ($userInfo['account'] == $give_account) {
                $this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '不可赠送给自己'
                ));
            }else{
                if ($userInfo['key_dynamic'] <= 0 || $userInfo['key_dynamic'] < $give_num) {
                    $this->ajaxReturn(array(
                        'code'=>0,
                        'msg' => '动态秘钥不足，无法赠送'
                    ));
                }else{
                    if ($userInfo['safe_password'] != md5($safe_password)) {
                        $this->ajaxReturn(array(
                            'code'=>0,
                            'msg' => '安全密码不正确'
                        ));
                    }else{
                        $where_account['account'] = $give_account;
                        $receiveInfo = $m_user->where($where_account)->find();
                        if (empty($receiveInfo['id'])) {
                            $this->ajaxReturn(array(
                                'code'=>0,
                                'msg' => '转出账号不存在'
                            ));
                        }else{
                            $old_keydynamic = $userInfo['key_dynamic'];
                            $change_keydynamic = $give_num;
                            $new_keydynamic = $old_keydynamic - $give_num;

                            $save['key_dynamic'] = $new_keydynamic;
                            $res = $m_user->where($where_user)->save($save);
                            if ($res) {
                                //添加动态秘钥记录
                                addKeyDynamicTable($userInfo['id'],$old_keydynamic,$change_keydynamic,$new_keydynamic,'赠送 '.$give_account.' 动态秘钥'.$change_keydynamic,2);
                                //添加赠送记录
                                addGivingTable($userInfo['id'],$receiveInfo['account'],$receiveInfo['id'],$give_num);
                                
                                //接收用户修改数据、添加动态秘钥记录
                                $old_receive = $receiveInfo['key_dynamic'];
                                $change_receive = $give_num;
                                $new_receive = $old_receive + $change_receive;

                                $save_receive['key_dynamic'] = $new_receive;
                                $result = $m_user->where(array('id'=>$receiveInfo['id']))->save($save_receive);
                                if ($result) {
                                    addKeyDynamicTable($receiveInfo['id'],$old_receive,$change_receive,$new_receive,$userInfo['account'].'赠送动态秘钥'.$change_receive);
                                }

                                $this->ajaxReturn(array(
                                    'code'=>1,
                                    'msg' => '操作成功'
                                ));
                            }else{
                                $this->ajaxReturn(array(
                                    'code'=>0,
                                    'msg' => '操作失败'
                                ));
                            }
                        }
                    }
                }
            }
        }else{
            $this->redirect('Common/login');
        }
    }


    /**
     * [givingrecreation 赠送娱乐积分]
     * @return [type] [description]
     */
    public function givingrecreation(){
        $uid = $_SESSION['uid'];

        $m_user = M('User');
        $where['id'] = $uid;

        $userInfo = $m_user->where($where)->find();

        $this->assign('userInfo',$userInfo);
        $this->display();
    }


    /**
     * [returnreport 转回报表]
     * @return [type] [description]
     */
    public function returnreport(){
        $this->display();
    }


    /**
     * [recommended 直推列表]
     * @return [type] [description]
     */
    public function recommended(){
        $uid = $_SESSION['uid'];

        $m_user = M('User');
        $where_user['id'] = $uid;

        $userInfo = $m_user->where($where_user)->find();


        $where['top_id'] = $uid;

        $count = $m_user->where($where)->count();
        $Page  = new \Think\Page($count,20);
        $show  = $Page->show();

        $recommendList = $m_user->where($where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign('page',$show);
        $this->assign('recommendList',$recommendList);
        $this->assign('userInfo',$userInfo);
        $this->display();
    }


    /**
     * [register 注册会员]
     * @return [type] [description]
     */
    public function register(){
        $uid = $_SESSION['uid'];

        $m_user = M('User');
        $where['id'] = $uid;

        $userInfo = $m_user->where($where)->find();

        $this->assign('userInfo',$userInfo);
        $this->display();
    }


    




}